<?php 
//all the class/function includes are managed in the includer file
include("include/includer.php");
//page specific includes
//RSS Feed Parser
require_once('magpierss72/rss_fetch.inc');

//get all the configuration variables (custom function in newzbook.php include)
getConfigVars();

//if the form was just posted back, let's use the form variables - otherwise the ones above will be used
if(isset($_GET['q'])){
    $q = str_replace(' ','+',$_GET['q']);
}
if(isset($_GET['area'])){
    $area = $_GET['area'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
    <?php standardHead("Search"); ?>
</HEAD>

<BODY>
<?php internetSecurity(); ?>
<div style="visibility:hidden;display:none;" id="progress"><div id="progress_bg"></div><a name="loading" /><div id="progress_box"><img id="progress_image" style="padding-left:5px;padding-top:5px;" src="images/ajax-loader.gif" alt="loading...">You request is being processed...</div></div>
<div class="headercontent">
  <?php headerContent(); ?>
</div>
<div class="maincontent">
<?php
if(!is_dir($config_NZBDropPath)) {
  echo "<div class='warningmessage'>The NZB Drop Path directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
}

if($config_enablenzbget == "enable"){
  if(isset($config_NZBgetAppPath) && isset($config_NZBgetConfigPath)){
    if(!is_dir($config_NZBgetAppPath)) {
        echo "<div class='warningmessage'>The NZBGet Application directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
        }
    if(!is_dir($config_NZBgetConfigPath)){
        echo "<div class='warningmessage'>The NZBGet Configuration directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
        }
  }
}
?>
  <div class="block">
    <h2>Newzbin Search</h2>
    <form action="nzbsearch.php" method="get">
    <input type="text" name="q" id="q" value="<?php if(isset($_GET['q'])){echo $q;}?>"/>
    <select name="area" size="1">
        <option <?php if($area == "-1"){echo "selected ";}?>value="-1">Everything</option>
        <optgroup label="Categories">
          <option <?php if($area == "c.0"){echo "selected ";}?>value="c.0" label="Unknown">Unknown</option>
          <option <?php if($area == "c.11"){echo "selected ";}?>value="c.11" label="Anime">Anime</option>
          <option <?php if($area == "c.1"){echo "selected ";}?>value="c.1" label="Apps">Apps</option>
          <option <?php if($area == "c.13"){echo "selected ";}?>value="c.13" label="Books">Books</option>
          <option <?php if($area == "c.2"){echo "selected ";}?>value="c.2" label="Consoles">Consoles</option>
          <option <?php if($area == "c.15"){echo "selected ";}?>value="c.15" label="Discussions">Discussions</option>
          <option <?php if($area == "c.10"){echo "selected ";}?>value="c.10" label="Emulation">Emulation</option>
          <option <?php if($area == "c.4"){echo "selected ";}?>value="c.4" label="Games">Games</option>
          <option <?php if($area == "c.5"){echo "selected ";}?>value="c.5" label="Misc">Misc</option>
          <option <?php if($area == "c.6"){echo "selected ";}?>value="c.6" label="Movies">Movies</option>
          <option <?php if($area == "c.7"){echo "selected ";}?>value="c.7" label="Music">Music</option>
          <option <?php if($area == "c.12"){echo "selected ";}?>value="c.12" label="PDA">PDA</option>
          <option <?php if($area == "c.14"){echo "selected ";}?>value="c.14" label="Resources">Resources</option>
          <option <?php if($area == "c.8"){echo "selected ";}?>value="c.8" label="TV">TV</option>
        </optgroup>
        <?php
        if($config_saved_searches != ""){
            echo "<optgroup label='SavedSearches'>";
              $savedsearches = explode(",",$config_saved_searches);
              foreach($savedsearches as $search){
                  //break out the ID and text
                  list($searchID, $searchname) = explode(":",$search);
                  //create the option
                  echo "<option ";
                  //if it's selected, mark it as so
                  if($area == "ss.".$searchID){echo "selected ";}
                  //fill out the details of the option
                  echo "value='ss.$searchID' label='$searchname'>$searchname</option>";
              }
            echo "</optgroup>";
        }
        ?>
    </select>
    <input type="submit" value="Search" onclick="return loadSubmit()"/>
    </form>
  </div>
<?php if($config_newzuname == "" || $config_newzpw == ""){ die("<div class='warningmessage'>Username and/or password are not set. Please set the username/password on the <a href='index.php'>configuration page</a>.</div>"); }
//if(preg_match('/^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/',$config_newzuname)){die("<div class='warningmessage'>Sorry, an e-mail address cannot currently be used for authentication with the Newzbin search. Please enter your Newzbin username in the configuration.</div>");}?>
<?php
//get the search query variable
if(isset($_GET['q'])){
  if($q==""){die("<div class='infomessage'>Empty searches are disabled.</div>");}
  echo "<div class='block'>
    <h2>Results</h2>";
      echo "You searched for <font size='+1'><strong>$q</strong></font>.<br /><br />";
      //post the search to newzbin    
      $url = 'http://'.$config_newzuname.':'.$config_newzpw.'@www.newzbin.com/search/query/?q='.$q.'&area='.$area.'&fpn=p&searchaction=Go&feed=rss&hauth=1';
      $rss = fetch_rss( $url );
      //parse out the fields and create the HTML for the page
      foreach ($rss->items as $item) {      
        //create the HTML output
        echo "<div class='nzbreport'>";
        //Report Title
        preg_match("/\(Size\: (.+)\)/",$item['description'],$matches);
        echo "<div class='reporttitle'>".$item['title']."<div class='reporttitle_smaller'>(".$matches[1].")</div></div><br />";
        //Report Description - Attributes
        $tmpVar = explode("<li>",$item['description']);
        echo str_replace("</li>","",$tmpVar[2])."<br />";        
        //Report Action Links
        echo "<a href='".$item['guid']."'>View Report on Newzbin</a> | ";
        //if nzbget is enabled, lets send the user to the nzbget page
        if($config_enablenzbget == "enable"){ 
            echo "<a href='nzbget.php?reportid=".$item['report']['id']."' onclick='return loadSubmit()'>Download Now</a>"; 
        }
        else { 
            echo "<a href='index.php?action=reportactual&reportid=".$item['report']['id']."' onclick='return loadSubmit()'>Download Now</a>"; 
        }
        if($item['report']['nfo_link'] != "" || $item['report']['moreinfo'] != ""){
            echo " | ";
        }
        if($item['report']['nfo_link'] != ""){
            echo "(<a href='".$item['report']['nfo_link']."'>NFO</a>) ";
        }
        if($item['report']['moreinfo'] != ""){
            echo "(<a href='".$item['report']['moreinfo']."'>More Info</a>)";        
        }
        //echo "Description: $rss_description <br />";
        echo "</div><br />";
      }
  echo "</div>";
}
?>
</div>
</BODY>